package com.red.wood.task;

import java.sql.Connection;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.red.wood.model.SqlInfo;
import com.red.wood.util.CommonUtils;
import com.red.wood.util.DateUtil;
import com.red.wood.util.JdbcUtil;
import com.red.wood.util.SqlParser;

public class StatisticsTask {
	
	private static final Log log = LogFactory.getLog(StatisticsTask.class);
	
	public static void doTask(SqlInfo sqlInfo,String day) {
		Connection isdmsOracleConn = null;
		try {
			log.info("=========数据汇总任务开始==========");

			isdmsOracleConn = JdbcUtil.getConnection();

			// 删除当前统计数据
			String delSql = "delete from t_st_ip_verify_base_day where day='" + day + "'";
			JdbcUtil.execSql(isdmsOracleConn, delSql);

			String sqlIpBaseSt = SqlParser.getSql(sqlInfo.getSqlIpBaseSt(), "day", day);
			JdbcUtil.execSql(isdmsOracleConn, sqlIpBaseSt);

			delSql = "delete from t_st_domain_verify_base_day where day='" + day + "'";
			JdbcUtil.execSql(isdmsOracleConn, delSql);

			String sqlDomainBaseSt = SqlParser.getSql(sqlInfo.getSqlDomainBaseSt(), "day", day);
			JdbcUtil.execSql(isdmsOracleConn, sqlDomainBaseSt);

			log.info("=========数据汇总任务完成==========");
		} catch (Exception e) {
			log.info("=========数据汇总任务异常==========");
			if (log.isErrorEnabled()) {
				log.error(e.getMessage(), e);
			}
		} finally {
			JdbcUtil.close(isdmsOracleConn);
		}
	}
	
	public static void main(String[] args) {
		//java -cp x.jar com.xx
		SqlInfo sqlInfo = CommonUtils.getSqlInfo();
		String day = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getYesterday());
		doTask(sqlInfo,day);
	}
}